/* main.c
 *
 * AUTHOR: John Wang
 * VERSION: 0.1  (20 Mar 2010)
 *
 * DESCRIPTION:
 * Configures a TReX at 19200 baud
 *
 */
 
#include <avr/io.h>
#include <avr/interrupt.h>

#include "uart.h"
#include "LCD.h"


#define TREX_BAUDRATE 19200
#define BAUD_PRESCALE (((F_CPU / (TREX_BAUDRATE * 16UL))) - 1) 

int main (void) {

	uart_init();
	LCDInit();

	// Set the baudrate
	UBRR0L = BAUD_PRESCALE;
	UBRR0H = (BAUD_PRESCALE >> 8);
		
	// enable global interrupts
	sei();
		
	uart_tx(0xAF);   // set configuration parameter
	uart_tx(0x7E);   // set UART settings
	uart_tx(0x08);   // no error checking, 1 stop bit, 57600 baud
	uart_tx(0x55);
	uart_tx(0x2A);
	
	LCDString("Waiting for response...");
	LCDMoveCursor(LCD_ROW_1);

	// wait for data
	while (!uart_hasData());
	
	unsigned char returnVal = uart_rx();
	if (returnVal == 0)
		LCDAddString("OK!");
	else if (returnVal == 3)
		LCDAddString("Error: TReX not in serial mode");
	else {
		LCDAddString("Error code ");
		LCDUInt(returnVal);
	}

	// end: do nothing
	while (1);
	//*/
}
